package javacommon.util;

import java.io.UnsupportedEncodingException;

public class JPC {
	/**
	 * 将对象转换成字符串
	 * @param obj 要转换的对象
	 * @return 字符串
	 * @throws UnsupportedEncodingException
	 */			
	public static String view(Object obj){
		if (obj == null){
			return "";
		}else{
	        return obj.toString();
		}
	}	
	
	public static String view(Object obj,String source , String target) throws UnsupportedEncodingException {
		if (obj == null){
			return "";
		}else{
	        return encode(obj.toString(),source,target);
		}
	}
	
	/**
	 * 判断输入的字符串是否为null,如果为null，则将输入字符串用缺省值字符串替换。
	 * @param nvlString 输入字符串
	 * @param defaultValue 缺省值字符串
	 * @return
	 */
	public static String nvl(String nvlString,String defaultValue){
		return nvlString==null?defaultValue:nvlString;
	}
	public static String encode(Object obj) throws UnsupportedEncodingException{
		return encode(view(obj));
	}
	public static String encode(Object obj,String source,String target) throws UnsupportedEncodingException{		
		return encode(view(obj),source,target);
	}
	public static String encode(String str) throws UnsupportedEncodingException{
		return encode(view(str),"ISO-8859-1","GBK");		
	}
	
	public static String encode(String str , String source,String target) throws UnsupportedEncodingException{		
		return new String(view(str).getBytes(source),target);
	}
}
